package com.lfy.commons.config.exception;

import com.lfy.common.enums.BaseEnum;
import com.lfy.common.result.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MultipartException;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionConfig {
    /**
     * 文件大小限制异常
     */
    @ExceptionHandler(value = MultipartException.class)
    public R fileUploadExceptionHandler(MultipartException exception) {
        String msg="";
        Throwable rootCase=exception.getRootCause();
        if (rootCase instanceof org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException) {
            msg="上传文件过大[文件大小不得超过" + "10MB" + "]";
        }else if(rootCase instanceof org.apache.tomcat.util.http.fileupload.impl.SizeLimitExceededException){
            msg="文件上传失败[请求字节超限]";
        }else {
            msg="上传失败[文件不合法]";
        }
        log.error(msg, exception);
        return R.error(msg,500);
    }
    /**
     * GET POST
     */
    @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
    public R FourZeroFiveError(HttpRequestMethodNotSupportedException e){
        return R.error(BaseEnum.commonEnum.POST_OR_GET);
    }

    /**
     * validation参数校验异常
     */
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public R methodArgumentNotValidExceptionExceptionHandler(MethodArgumentNotValidException e) {
        StringBuilder errorMsg = new StringBuilder();
        e.getBindingResult().getFieldErrors().forEach(x -> errorMsg.append(x.getField()).append(x.getDefaultMessage()).append(","));
        String message = errorMsg.toString();
        log.info("validation parameters error！The reason is:{}", message);
        return R.error( message.substring(0, message.length() - 1),BaseEnum.commonEnum.PARAM_VALID.getCode());
    }

    /**
     * validation参数校验异常
     */
    @ExceptionHandler(value = BindException.class)
    public R bindException(BindException e) {
        StringBuilder errorMsg = new StringBuilder();
        e.getBindingResult().getFieldErrors().forEach(x -> errorMsg.append(x.getField()).append(x.getDefaultMessage()).append(","));
        String message = errorMsg.toString();
        log.info("validation parameters error！The reason is:{}", message);
        return R.error( message.substring(0, message.length() - 1),BaseEnum.commonEnum.PARAM_VALID.getCode());
    }

    /**
     * 处理空指针的异常
     */
    @ExceptionHandler(value = NullPointerException.class)
    public R exceptionHandler(NullPointerException e) {
        log.error("null point exception！The reason is: ", e);
        return R.error(BaseEnum.commonEnum.SYSTEM_ERROR.getMsg());
    }

    /**
     * 未知异常
     */
    @ExceptionHandler(value = Exception.class)
    public R systemExceptionHandler(Exception e) {
        log.error("system exception！The reason is：{}", e.getMessage(), e);
        return R.error(BaseEnum.commonEnum.SYSTEM_ERROR);
    }
}
